package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IdUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.service.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.mapper.SysCompanyMapper;
import com.ruoyi.system.domain.SysCompany;
import com.ruoyi.system.service.ISysCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公司信息Service业务层处理
 *
 * @author Ele
 * @date 2021-02-04
 */
@Service
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements ISysCompanyService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private RedisService redisService;
    @Override
    public List<SysCompany> queryList(SysCompany sysCompany) {
        LambdaQueryWrapper<SysCompany> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sysCompany.getCompanyName())) {
            lqw.like(SysCompany::getCompanyName, sysCompany.getCompanyName());
        }
        if (StringUtils.isNotBlank(sysCompany.getAddress())) {
            lqw.like(SysCompany::getAddress, sysCompany.getAddress());
        }
        if (StringUtils.isNotBlank(sysCompany.getPhone())) {
            lqw.eq(SysCompany::getPhone, sysCompany.getPhone());
        }
        if (StringUtils.isNotBlank(sysCompany.getEmail())) {
            lqw.eq(SysCompany::getEmail, sysCompany.getEmail());
        }
        if (sysCompany.getActiveTime() != null) {
            lqw.eq(SysCompany::getActiveTime, sysCompany.getActiveTime());
        }
        if (sysCompany.getActiveFlag() != null) {
            lqw.eq(SysCompany::getActiveFlag, sysCompany.getActiveFlag());
        }
        if (StringUtils.isNotBlank(sysCompany.getTempId())) {
            lqw.eq(SysCompany::getTempId, sysCompany.getTempId());
        }
        if (sysCompany.getComCode() != null) {
            lqw.eq(SysCompany::getComCode, sysCompany.getComCode());
        }
        if (StringUtils.isNotBlank(sysCompany.getRegionCodes())) {
            lqw.eq(SysCompany::getRegionCodes, sysCompany.getRegionCodes());
        }
        return this.list(lqw);
    }
    @Override
    public void addCompany(SysCompany sysCompany) {
        if (null == sysCompany.getActiveFlag()) {
            sysCompany.setActiveFlag(Constants.FLAG_YES.longValue());
        }
        if (StringUtils.isBlank(sysCompany.getPhone())) {
            throw new CustomException("电话不能为空");
        }
        if (null == sysCompany.getTempId()) {
            throw new CustomException("请选择权限模板");
        }
        sysCompany.setComCode(Long.valueOf(IdUtils.genComCode()));
        if (!save(sysCompany)) {
            throw new CustomException("添加公司失败");
        }else {

            SysUser user = getSysUser(sysCompany);
            AjaxResult add = userFeign.add(user);
            if(!add.get("code").equals(HttpStatus.SUCCESS)){
                throw new CustomException("添加公司管理员失败:"+add.get("msg"));
            }
        }
    }

    @Override
    public void resetPwd(String phonen,String resetPwd) {
        SysUser sysUser = new SysUser();
        sysUser.setPassword(resetPwd);
        sysUser.setUserName(phonen);
        AjaxResult edit = userFeign.edit(sysUser);
        if(!edit.get("code").equals(HttpStatus.SUCCESS)){
           throw  new CustomException(edit.get("msg").toString());
        }
    }

    private SysUser getSysUser(SysCompany sysCompany) {
        String initPassword = redisService.getCacheObject(Constants.SYS_CONFIG_KEY + "sys.user.initPassword");
        SysUser user = new SysUser();
        user.setUserName(sysCompany.getPhone());
        user.setPhonenumber(sysCompany.getPhone());
        user.setNickName("管理员" + sysCompany.getComCode());
        user.setEmail(sysCompany.getEmail());
        user.setStatus(Constants.FLAG_NO.toString());//0正常1停用
        user.setCreateTime(new Date());
        user.setRemark("公司管理员账号");
        user.setComId(sysCompany.getId());
        user.setAdminFlag(Constants.FLAG_YES);//是否管理员0否1是
        user.setSuperAdminFlag(Constants.FLAG_NO);//是否平台超级管理员
        user.setPassword(initPassword);
        return user;
    }
}
